package org.itfarmer.service.impl;

import java.util.List;

import org.itfarmer.dao.UserDAO;
import org.itfarmer.dao.pojo.User;
import org.itfarmer.service.UserService;

public class UserServiceImpl implements UserService {

	public UserDAO userDAO;
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.BaseService#countService()
	 */
	public Integer countService() {
		Integer rows = userDAO.count(User.class);
		return rows;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#register(org.itfarmer.dao.pojo.User)
	 */
	public boolean register(User user) {
		if (user == null) {
			return false;
		}
		Integer pk = userDAO.create(user);
		return pk != null && pk > 0 ? true : false;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#login(java.lang.Integer, java.lang.String)
	 */
	public User login(Integer workid, String password) {
		if (workid == null || workid <= 0 || password == null) {
			return null;
		}
		User userProxy = userDAO.read(User.class, workid);
		if (userProxy.getPassword().equals(password)) {
			return userProxy;
		} else {
			return null;
		}
	}
 
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#searchByWorkid(java.lang.Integer)
	 */
	public User searchByWorkid(Integer workid) {
		if (workid == null || workid <= 0) {
			return null;
		}
		User userProxy = userDAO.read(User.class, workid);
		return userProxy;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#searchByEmail(java.lang.String)
	 */
	public User searchByEmail(String email) {
		if (email == null) {
			return null;
		}
		User user = userDAO.readByEmail(email);
		return user;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#searchByPhone(java.lang.String)
	 */
	public User searchByPhone(String phone) {
		if (phone == null) {
			return null;
		}
		User user = userDAO.readByPhone(phone);
		return user;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.UserService#searchAll(java.lang.Integer, java.lang.Integer)
	 */
	public List<User> searchAll(Integer pageSize, Integer pageNow) {
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List<User> userList = userDAO.read(User.class, firstRow, maxRow);
		return userList;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	
}
